package grape.workflow.rest.task.vo;

import grape.common.service.trans.TransBy;
import grape.common.service.trans.impl.DatetimeTransServiceImpl;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.flowable.cmmn.api.repository.CaseDefinition;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.ui.task.model.runtime.TaskRepresentation;

import java.util.Date;
import java.util.Optional;

/**
 * 原生响应扩展
 * Created by yangwei
 * Created at 2020/6/16 20:03
 */
@Data
@EqualsAndHashCode(callSuper=false)
@ApiModel(value="任务数据响应原生扩展对象")
public class TaskRepresentationVo extends TaskRepresentation {

    public TaskRepresentationVo() {
    }

    public TaskRepresentationVo(Task task) {
        super(task);
    }

    public TaskRepresentationVo(HistoricTaskInstance task) {
        super(task);
    }

    public TaskRepresentationVo(TaskInfo taskInfo, ProcessDefinition processDefinition) {
        super(taskInfo, processDefinition);
    }

    public TaskRepresentationVo(TaskInfo taskInfo, CaseDefinition caseDefinition) {
        super(taskInfo, caseDefinition);
    }

    public TaskRepresentationVo(TaskInfo taskInfo, TaskInfo parentTaskInfo) {
        super(taskInfo, parentTaskInfo);
    }

    public TaskRepresentationVo(TaskInfo taskInfo, ProcessDefinition processDefinition, String processInstanceName) {
        super(taskInfo, processDefinition, processInstanceName);
    }

    public TaskRepresentationVo(TaskInfo taskInfo, CaseDefinition caseDefinition, String caseInstanceName) {
        super(taskInfo, caseDefinition, caseInstanceName);
    }

    @Override
    public void initializeTaskDetails(TaskInfo taskInfo) {
        super.initializeTaskDetails(taskInfo);
        this.claimTime = taskInfo.getClaimTime();
        if(taskInfo instanceof Task){
            this.delegateState = Optional.ofNullable(((Task) taskInfo).getDelegationState()).map(delegationState -> delegationState.name()).orElse(null);
            this.delegateStateName = TaskTool.getDelegateStateNameMap().get(this.delegateState);

        }
    }

    private String delegateState;

    private String delegateStateName;


    @ApiModelProperty(value = "签收时间")
    private Date claimTime;

    @TransBy(type = DatetimeTransServiceImpl.trans_datetime,byFieldName = "claimTime")
    @ApiModelProperty(value = "签收时间字符串")
    private String claimTimeTxt;
    /**
     * 任务开始时间
     */
    @ApiModelProperty(value = "任务创建时间字符串")
    @TransBy(type = DatetimeTransServiceImpl.trans_datetime,byFieldName = "created")
    private String createdStr;

    /**
     * 任务结束时间
     */
    @ApiModelProperty(value = "任务结束时间字符串")
    @TransBy(type = DatetimeTransServiceImpl.trans_datetime,byFieldName = "endDate")
    private String endDateStr;

    @ApiModelProperty(value = "是否可撤回",notes = "仅针对历史已办任务")
    private Boolean isCanRecall;
    /**
     * 业务编号
     */
    @ApiModelProperty(value = "业务编号")
    private String businessKey;
    /**
     * 流程开始时间
     */
    @ApiModelProperty(value = "流程开始时间")
    private Date processStartAt;

    @ApiModelProperty(value = "流程开始时间字符串")
    @TransBy(type = DatetimeTransServiceImpl.trans_datetime,byFieldName = "processStartAt")
    private String processStartAtTxt;
    /**
     * 流程结束时间，如果没有结束时间，说明流程尚未结束
     */
    @ApiModelProperty(value = "流程结束时间")
    private Date processEndAt;

    @ApiModelProperty(value = "流程结束时间字符串")
    @TransBy(type = DatetimeTransServiceImpl.trans_datetime,byFieldName = "processEndAt")
    private String processEndAtTxt;

    @ApiModelProperty(value = "第一条任务评论")
    private String firstCommentMessage;

}
